﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASKM.Rachao.Model.Helper;
using ASKM.Rachao.Model.Extensions;

namespace ASKM.Rachao.Model.Domain
{
    /// <summary>
    /// Domain class for security
    /// </summary>
    public class Security: BaseDomain
    {
        #region Attributes

        private string _ticketUrl = String.Format("{0}PlayerProfile/InitialRegister/",ConfigHelper.GetBaseUrl());

        #endregion

        /// <summary>
        /// Get formatted string for ticket 
        /// </summary>
        /// <param name="pp">PlayerProfile</param>
        /// <returns>
        /// Formatted string for aproval / reproval encrypted string
        /// </returns>
        private string GetStringFromTicket(PlayerProfile pp)
        {
            var ppat = (from x in this.CtxRachao.PlayerProfileApprovalTicket
                        where x.PlayerProfileId == pp.PlayerProfileId
                        orderby x.PlayerProfileApprovalTicketId descending
                        select x).FirstOrDefault();

            if (ppat != null)
                return String.Concat(ppat.GeneratedDate.ToString("ddMMyyyy"),ppat.ValidDate.ToString("ddMMyyyy"),ppat.PlayerProfileId);
            return String.Empty;
        }

        /// <summary>
        /// Get encrypted string for user approval
        /// </summary>
        /// <param name="pp">PlayerProfile</param>
        /// <returns>
        /// Encrypted string for approval
        /// </returns>
        public string GetStringUrlForApproval(PlayerProfile pp)
        {
            var ret = this.GetStringFromTicket(pp);
            if (!String.IsNullOrEmpty(ret))
                return String.Concat(this._ticketUrl,Intentor.Utilities.CryptoHelper.Encrypt(String.Concat("1",ret)));
            return String.Empty;
        }

        /// <summary>
        /// Get encrypted string for user reproval
        /// </summary>
        /// <param name="pp">PlayerProfile</param>
        /// <returns>
        /// Encrypted string for reproval
        /// </returns>
        public string GetStringUrlForReproval(PlayerProfile pp)
        {
            var ret = this.GetStringFromTicket(pp);
            if (!String.IsNullOrEmpty(ret))
                return String.Concat(this._ticketUrl, Intentor.Utilities.CryptoHelper.Encrypt(String.Concat("0",ret)));
            return String.Empty;
        }

        /// <summary>
        /// Get a Ticket object from a encrypted ticket
        /// </summary>
        /// <param name="ticket">Encrypted ticket</param>
        /// <returns>PlayerProfileApprovalTicket</returns>
        public PlayerProfileApprovalTicket GetTicketFromString(string ticket)
        {
            var ticketDecrypted = Intentor.Utilities.CryptoHelper.Decrypt(ticket);
            bool isApproved = Convert.ToBoolean(Convert.ToInt32(ticketDecrypted.Substring(0, 1)));
            DateTime initialDate = DateTime.ParseExact(ticketDecrypted.Substring(1, 8), "ddMMyyyy", null).ToFirstHour();
            DateTime finalDate = DateTime.ParseExact(ticketDecrypted.Substring(9, 8), "ddMMyyyy", null).ToLastHour();
            Int32 pid = Convert.ToInt32(ticketDecrypted.Substring(17));

            PlayerProfileApprovalTicket ppat = new PlayerProfileApprovalTicket();
            ppat.GeneratedDate = initialDate;
            ppat.ValidDate = finalDate;
            ppat.PlayerProfileId = pid;

            return ppat;
        }

        /// <summary>
        /// Get approval status from ticket
        /// </summary>
        /// <param name="ticket">Encrypeted ticket</param>
        /// <returns>
        /// True - approved
        /// False - not approved
        /// </returns>
        public bool GetApprovalStatusFromTicketString(string ticket)
        {
            var ticketDecrypted = Intentor.Utilities.CryptoHelper.Decrypt(ticket);
            bool isApproved = Convert.ToBoolean(Convert.ToInt32(ticketDecrypted.Substring(0, 1)));
            return isApproved;
        }

    }
}
